<?php
require_once('../../libraryfiles/config.php');


$action = isset($_GET['action']) ? $_GET['action'] : '';

switch($action){
	case 'setting' :
		UpdateFreeAccountLimitProduct();
		break;

	default :
		header('Location:'.base_url_admin.'/setting/index.php');
		exit();
}
function UpdateFreeAccountLimitProduct(){
	
	if(isset($_POST['btnSaveSetting'])){
		if(!empty($_POST['store_site_title'])){
			UpdateGlobalSetting($_POST['store_site_title'],$where='site_title');
		}
		if(!empty($_POST['meta_tag_keywords'])){
			UpdateGlobalSetting($_POST['meta_tag_keywords'],$where='meta_tage_keyword');
		}
		if(!empty($_POST['meta_tag_description'])){
			UpdateGlobalSetting($_POST['meta_tag_description'],$where='meta_tage_description');
		}
		if(!empty($_POST['google_analytic_id'])){
			UpdateGlobalSetting($_POST['google_analytic_id'],$where='google_analytic_id');
		}
		if(!empty($_POST['google_analytics_id'])){
			UpdateGlobalSetting($_POST['google_analytics_id'],$where='product_limit_setting');
		}
		if(!empty($_POST['watermark_product'])){
			UpdateGlobalSetting($_POST['watermark_product'],$where='watermark');
		}
		if(!empty($_POST['product_limit_setting'])){
			UpdateGlobalSetting($_POST['product_limit_setting'],$where='free_account');
		}
		if(!empty($_POST['top_province'])){
			UpdateGlobalSetting($_POST['top_province'],$where='top_province');
		}
		if(!empty($_POST['top_district'])){
			UpdateGlobalSetting($_POST['top_district'],$where='top_district');
		}
		if(!empty($_POST['number_product_01'])){
			UpdateGlobalSetting($_POST['number_product_01'],$where='number_of_product1');
		}
		if(!empty($_POST['number_product_02'])){
			UpdateGlobalSetting($_POST['number_product_02'],$where='number_of_product2');
		}
		if(!empty($_POST['number_product_03'])){
			UpdateGlobalSetting($_POST['number_product_03'],$where='number_of_product3');
		}
		if(!empty($_POST['number_product_04'])){
			UpdateGlobalSetting($_POST['number_product_04'],$where='number_of_product4');
		}
		if(!empty($_POST['upload_image_size'])){
			UpdateGlobalSetting($_POST['upload_image_size'],$where='upload_file_size');
		}
		if(!empty($_POST['mail_server'])){
			UpdateGlobalSetting($_POST['mail_server'],$where='mail_server');
		}
		if(!empty($_POST['account_login'])){
			UpdateGlobalSetting($_POST['account_login'],$where='mail_user_login');
		}
		if(!empty($_POST['account_password'])){
			UpdateGlobalSetting($_POST['account_password'],$where='mail_user_password');
		}
		if(!empty($_POST['mail_port'])){
			UpdateGlobalSetting($_POST['mail_port'],$where='mail_port');
		}
		if(!empty($_POST['ischeckedVisitors'])){
			UpdateGlobalSetting($_POST['ischeckedVisitors'],$where='visitors');
		}else{
			UpdateGlobalSetting(0,$where='visitors');
		}
		if(!empty($_FILES["logo_change"]["name"])){
			$image =  $_FILES["logo_change"]["name"];
			$imagePath = '../../images/';
			$allowedExts = array("gif", "jpeg", "jpg", "png","GIF","JPEG","JPG","PNG");
			$temp = explode(".", $_FILES["logo_change"]["name"]);
			$extension = end($temp);
			if(!in_array($extension, $allowedExts)){
				header("location:".base_url_admin."/setting/index.php?view=add&message=invalidextention");
				exit();
			}
			$imageSize = SelectOnlyIMG($where='upload_file_size');
			if($_FILES["logo_change"]["size"] > 1048576*$imageSize){
				header("location:".base_url_admin."/setting/index.php?view=add&message=invalidsize");
				exit();
			}
		if($_FILES["logo_change"]["size"] == 0){
				header("location:".base_url_admin."/setting/index.php?view=add&message=invalidsize");
				exit();
			}
			if (file_exists($imagePath . $_FILES["logo_change"]["name"])){
				$fname = random(5) . '-' . $image;
				move_uploaded_file($_FILES["logo_change"]["tmp_name"],$imagePath . $fname);
				$Img_name = SelectOnlyIMG($where='logo');
				unlink($imagePath.$Img_name);
				UpdateGlobalSetting($fname,$where='logo');
			}else{
				$fname = $image;
				$Img_name = SelectOnlyIMG($where='logo');
				unlink($imagePath.$Img_name);
				move_uploaded_file($_FILES["logo_change"]["tmp_name"],$imagePath . $fname);
				UpdateGlobalSetting($fname,$where='logo');
			}
		}
		header("location:".base_url_admin."/setting/index.php?view=add&message=success");
	}

}

function UpdateGlobalSetting($data,$where){
	$Sql = "UPDATE tbl_refcode SET ref_value='".trim($data)."' WHERE ref_name='".$where."'";
	$getSiteTile = mysql_query($Sql);
}
function SelectOnlyIMG($where){
	$rows = '';
	$Sql = "SELECT ref_value FROM tbl_refcode WHERE ref_name='".$where."' LIMIT 1";
	$getSiteTile = mysql_query($Sql);
	$getResult = mysql_fetch_object($getSiteTile);
	$rows = $getResult->ref_value;
	if(!empty($rows)){
		return $rows;
	}else{
		return $rows;
	}
}
?>